/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.identity.domain;

import com.jb.framework.database.Predicate;

import com.jb.web.context.WebContext;
import com.jb.web.model.DBDescriptor;
import com.jb.framework.ModelBean;

import com.jb.framework.PersistentManagerRegistry;

import java.util.Calendar;
import java.util.List;


public class GroupMembership extends ModelBean {
    private String groupId;
    private String groupName;
    private String userId;
    
    private String type;

    public static void join(WebContext ctx, UserAccount user, Group group) {
        GroupMembership newMembership = (GroupMembership) PersistentManagerRegistry.findById(GroupMembership.class,
                group.getId() + user.getId());

        if (newMembership != null) {
            return; //already a member
        }

        newMembership = new GroupMembership();
        newMembership.setGroupId(group.getId());
        newMembership.setId(group.getId() + user.getId());
        newMembership.setGroupName(group.getNickName());
        newMembership.setUserId(user.getId());
        newMembership.setStartDate(Calendar.getInstance().getTime());
        newMembership.setType("Normal");
        newMembership.create(ctx);
    }

    public static List getMemberships(WebContext ctx, String userId) {
        return PersistentManagerRegistry.find(GroupMembership.class, new Predicate("userId", userId, "="));
    }

    public static void close(WebContext ctx, String userId, String groupId) {
        GroupMembership newMembership = (GroupMembership) PersistentManagerRegistry.findById(GroupMembership.class,
                userId + groupId);

        if (newMembership != null) {
            newMembership.setEndDate(Calendar.getInstance().getTime());
            newMembership.update(ctx);
        }
    }

    public void setGroupName(String groupId) {
        this.groupId = groupId;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String getGroupName() {
        return groupName;
    }

    /**
     * DOCUMENT ME!
     *
     * @param userId DOCUMENT ME!
     */
    public void setUserName(String userId) {
        this.userId = userId;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String getUserName() {
        return userId;
    }

    /**
     * DOCUMENT ME!
     *
     * @param type DOCUMENT ME!
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String getType() {
        return type;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserId() {
        return userId;
    }

    static class _DBDescriptor extends DBDescriptor {
        static {
            PersistentManagerRegistry.registerDescriptor(Friendship.class, new _DBDescriptor());
        }

        _DBDescriptor() {
            addIndex("membership", new String[] { "userid, groupid" });
        }
    }
}
